package com.ete.service.net.http.param

import okhttp3.MediaType
import okhttp3.RequestBody
import java.io.File

/**
 * Author:LiXian
 * <p>
 * Desc:参数辅助类
 * Version:1.0
 * date 2024/1/15 14:59
 */
class ParamHelper {

    private val params: HashMap<String, RequestBody> = HashMap()

    companion object {
        @JvmStatic
        fun getInstance(): ParamHelper = ParamHelper()
    }

    /**
     * 清空参数
     */
    fun clearParameter() {
        params.clear()
    }

    /**
     * 添加参数
     * 根据传进来的Object对象来判断是String还是File类型的参数
     */
    fun addParameter(key: String, o: Any): ParamHelper {
        if (o is String) {
            val body: RequestBody = RequestBody.create(MediaType.parse("text/plain;charset=UTF-8"), o)
            params[key] = body
        } else if (o is File) {
            val body: RequestBody = RequestBody.create(MediaType.parse("multipart/form-data;charset=UTF-8"), o)
            params[key + "\"; filename=\"" + o.name + ""] = body
        }
        return this
    }

    /**
     * 构建RequestBody
     */
    fun builder(): HashMap<String, RequestBody> {
        return params
    }
}